set more off

foreach city in memphis nashville {

	local City = upper(substr("`city'", 1, 1)) + substr("`city'", 2, 50)

	foreach wave in 1 2 {

		use "dataset_`city' panel", clear
		
		if "`city'" == "memphis" local candidates ideoawgowhat`wave' ideocollins`wave' ideoanderson`wave' ideohodges`wave' ideostrickland`wave' ideowalker`wave' ideowebb`wave' ideowharton`wave' ideowilliams`wave' ideoself`wave'
		if "`city'" == "nashville" local candidates ideobarry`wave' ideokane`wave' ideobone`wave' ideorebrovick`wave' ideofreeman`wave' ideofox`wave' ideogentry`wave' ideoself`wave'

		if "`city'" == "memphis" local mytitle "`City'"
		if "`city'" == "nashville" local mytitle "`City', Wave `wave'"
		
		if "`city'" == "memphis" drop if mi(ideoself1) | mi(ideoself2) | mi(ideostrickland1) | mi(ideowharton1) | mi(ideostrickland2) | mi(ideowharton2) 
		if "`city'" == "nashville" drop if mi(ideoself1) | mi(ideoself2) | mi(ideobarry1) | mi(ideofox1) | mi(ideobarry2) | mi(ideofox2) 

		count if !mi(ideoself`wave')
		local N`city'`wave' `r(N)'
					
		collapse `candidates'
		foreach cand in `candidates' {
			local cand2 = subinstr("`cand'", "ideo", "", 10)
			local candidates2 `candidates2' `cand2'
		}	

		gen index = 1
		reshape i index
		reshape j candidate `candidates2'
		reshape xij ideo

		reshape long

		replace candidate = subinstr(candidate, "1", "", 10)
		replace candidate = subinstr(candidate, "2", "", 10)

		replace candidate = upper(substr(candidate, 1, 1)) + substr(candidate, 2, 50)
		
		#delimit;
		gen 
			labeled
				= 
				regexm(lower(candidate), "wharton") | 
				regexm(lower(candidate), "strickland") |
				regexm(lower(candidate), "self") |
				regexm(lower(candidate), "barry") |
				regexm(lower(candidate), "fox")
				;
		#delimit cr

		#delimit;

		gr tw
			(sc index ideo if labeled == 1, mlabel(candidate) mlabsize(vsmall) mlabpos(12) col(black) msym(O))
			,
				legend(off)
				plotregion(style(none))
				xlab(1(1)7)
				yscale(off)
				xtitle("")
				scale(2)
				nodraw
				name(g`city'`wave', replace)
				title("`mytitle'", size(small))
				;

		#delimit cr
		
	}

}

gr combine gmemphis1 gnashville1 gnashville2, imargin(zero) rows(3) xsize(5) ysize(3) xcommon b2title("Average ideological placement")

gr export "figure 2.eps", replace
shell epstopdf "figure 2.eps"

di `Nmemphis1'
di `Nnashville1'
di `Nnashville1'
